package com.sfac.javaSpringBoot.modules.test.controller;

import com.github.pagehelper.PageInfo;
import com.sfac.javaSpringBoot.modules.common.entity.Result;
import com.sfac.javaSpringBoot.modules.common.entity.SearchBean;
import com.sfac.javaSpringBoot.modules.test.entity.City;
import com.sfac.javaSpringBoot.modules.test.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description CityController
 * @Author HymanHu
 * @Date 2021/9/13 14:36
 */
@RestController
@RequestMapping("/api")
public class CityController {

    @Autowired
    private CityService cityService;

    /**
     * 127.0.0.1/api/city ---- post
     * {"cityName":"dreamCity","localCityName":"梦想之城","countryId":522}
     */
    @PostMapping(value = "/city", consumes = "application/json")
    public Result<City> insertCity(@RequestBody City city) {
        return cityService.insertCity(city);
    }

    /**
     * 127.0.0.1/api/city ---- put
     * cityName:dreamCity,localCityName=梦想之城1,cityId=2258
     */
    @PutMapping(value = "/city", consumes = "application/x-www-form-urlencoded")
    public Result<City> updateCity(@ModelAttribute City city) {
        return cityService.updateCity(city);
    }

    /**
     * 127.0.0.1/api/city/2258 ---- delete
     */
    @DeleteMapping("/city/{cityId}")
    public Result<Object> deleteCityByCityId(@PathVariable int cityId) {
        return cityService.deleteCityByCityId(cityId);
    }

    /**
     * 127.0.0.1/api/city/1890 ---- get
     */
    @GetMapping("/city/{cityId}")
    public City getCityByCityId(@PathVariable int cityId) {
        return cityService.getCityByCityId(cityId);
    }

    /**
     * 127.0.0.1/api/city?cityName=Shanghai&localCityName=1111 ---- get
     */
    @GetMapping("/city")
    public City getCityByCityNameAndLocalCityName(@RequestParam String cityName,
                                                  @RequestParam String localCityName) {
        return cityService.getCityByCityNameAndLocalCityName(cityName, localCityName);
    }

    /**
     * 127.0.0.1/api/cities ---- post
     * {"currentPage":1,"pageSize":5,"orderBy":"city_name","direction":"desc","keyword":""}
     */
    @PostMapping(value = "/cities", consumes = "application/json")
    public PageInfo<City> getCitiesBySearchBean(@RequestBody SearchBean searchBean) {
        return cityService.getCitiesBySearchBean(searchBean);
    }
}
